<?php
class League {
	public $id;
	public $name;
	public $year;
}

class Team {
	public $id;
	public $coachName;
	public $teamName;
	public $leagueId;
	public $teamId;
	public $usePreferences;
	public $isAdmin;
	public $teamSubmittedStatus;
	
	public $password;
	
	public function __toString() {
		return $this->coachName;
	}			
}

class PlayerDraft {
	public $round;
	public $teamId;
	public $pickNumber;
	public $coachNName;
	public $playerId;
	public $player;
	public $usePreferences;
	public $pickDateTime;
	public $state;
}

class TeamPlayer {
	public $pickNumber;
	public $coachNName;
	public $playerId;
	public $player;
	public $playerPosition;
	public $isLti;
	public $isLtiReplacement;
	public $positionInAFLTeam;
    public $playerInfo;
    public $roundsSincePlayed;
}

class Player {
	public $id;
	public $playerFirstName;
	public $playerSurname;
	public $team;
	public $playerType;
	
	public function getDescription() {
		return $this->playerFirstName . " " . $this->playerSurname . " (" . getAFLTeam($this->team) . ")";
	}
}

class PrefChanged {
	public $playerFirstName;
	public $playerSurname;
	public $team;
	public $state;
	
	public function getDescription() {
		return $this->playerFirstName . " " . $this->playerSurname . " (" . getAFLTeam($this->team ). ")";
	}
}

class SelectedTeam {
	public $id;
	public $teamId;
	public $teamName;
	public $yourPicks;
	public $ins;
	public $outs;
	
	function __construct($yourPicks, $teamName) {
		$this->yourPicks = $yourPicks;
		$this->teamName = $teamName;
	}
	
	public function getPlayer($position) {
		$count = count($this->yourPicks);
		for ($i = 0; $i < $count; $i++) {
			if ($this->yourPicks[$i]->playerPosition == $position) {
				return $this->yourPicks[$i];
			}
		}
	}
	
	public function isSelected($position) {
		$count = count($this->yourPicks);
		for ($i = 0; $i < $count; $i++) {
			if ($this->yourPicks[$i]->playerPosition == $position) {
				return $this->yourPicks[$i]->positionInAFLTeam != null;
			}
		}
	}
	
	public function getPlayers() {
		$array = array();
		$count = count($this->yourPicks);
		for ($i = 0; $i < $count; $i++) {
			if (!$this->yourPicks[$i]->isLti) {
				$array[] = $this->yourPicks[$i];
			}
		}
		
		return $array;
	}
	
	public function getReserves() {
		$array = array();
		$count = count($this->yourPicks);
		for ($i = 0; $i < $count; $i++) {
			if (($this->yourPicks[$i]->playerPosition == "RES" || $this->yourPicks[$i]->playerPosition == null) && !$this->yourPicks[$i]->isLti) {
				$array[] = $this->yourPicks[$i];
			}
		}
		
		return $array;
	}

	public function getLti() {
		$array = array();
		$count = count($this->yourPicks);
		for ($i = 0; $i < $count; $i++) {
			if ($this->yourPicks[$i]->isLti) {
				$array[] = $this->yourPicks[$i];
			}
		}
		
		return $array;
	}
	
	public function getLtiReplacements() {
		$array = array();
		$count = count($this->yourPicks);
		for ($i = 0; $i < $count; $i++) {
			if ($this->yourPicks[$i]->isLtiReplacement) {
				$array[] = $this->yourPicks[$i]->player;
			}
		}
		
		return $array;
	}	
	
	public function isLtiReplacement($id) {
		$count = count($this->yourPicks);
		for ($i = 0; $i < $count; $i++) {
			if ($this->yourPicks[$i]->player->id == $id && $this->yourPicks[$i]->isLtiReplacement) {
				return true;
			}
		}
		
		return false;
	}	
}

class LtiSpud {
	public $id;
	public $coachName;
	public $reasonType;
	public $playerOne;
	public $playerTwo;
	public $source;
	public $ltiState;
}

class Trade {
	public $id;
	public $partyOneId;
	public $partyOne;
	public $partyTwoId;
	public $partyTwo;
	public $state;
	public $proposeDateTime;
	public $responseDateTime;
	
	public $partyOneReceives = array();
	public $partyTwoReceives = array();
}

class TradeItem {
	public $itemType;
	public $player;
	public $pickNumber;
}

class TeamResult {
	public $team;
	public $gamesWon;
	public $gamesDrawn;
	public $forValue;
	public $againstValue;
	public $numberPlayersSelectedForCurrentRound;
	public $numberPlayersSelected;
	public $numberGames;
	
	public $lowestScore;
	public $highestScore;
	
	public $isWinningStreak;
	public $streak;
	
	function __construct($team) {
		$this->team = $team;
		$this->numberGames = 0;
		$this->lowestScore = 30000;
		$this->highestScore = 0;
		$this->isWinningStreak = false;
		$this->streak = 0;
	}
	
	public function addWin() {
		$this->gamesWon++;
	}
	
	public function addDraw() {
		$this->gamesDrawn++;
		if ($this->isWinningStreak) {
			$this->streak++;
		}
		else {
			$this->streak = 1;
			$this->isWinningStreak = true;
		}
	}
	
	public function doStreak($isWin) {
		if ($isWin) {
			if ($this->isWinningStreak) {
				$this->streak++;
			}
			else {
				$this->streak = 1;
				$this->isWinningStreak = true;
			}
		}
		else {
			if (!$this->isWinningStreak) {
				$this->streak++;
			}
			else {
				$this->streak = 1;
				$this->isWinningStreak = false;
			}
		}
	}
	
	public function getStreakVal() {
		if ($this->isWinningStreak) {
			return "W" . $this->streak;
		}
		else {
			return "L" . $this->streak;
		}
	}
	
	public function addFor($score) {
		$this->forValue += $score;
		$this->numberGames++;
		
		if ($score > $this->highestScore) {
			$this->highestScore = $score;
		}
		if ($score < $this->lowestScore) {
			$this->lowestScore = $score;
		}
	}
	
	public function addAgainst($score) {
		$this->againstValue += $score;
	}
	
	public function getPercentage() {
		if ($this->againstValue == 0) return 0;
		return round((floatval($this->forValue) / floatval($this->againstValue)) * 100, 2);
		//return new BigDecimal(((Double.valueOf(forValue) / Double.valueOf(againstValue)) * 100)).setScale(2, RoundingMode.DOWN).doubleValue();
	}

	public function getAverageScore() {
		if ($this->forValue == 0) return 0;
		
		return round(floatval($this->forValue) / floatval($this->numberGames));
		//return new BigDecimal(((Double.valueOf(forValue) / Double.valueOf(numberGames)))).setScale(2, RoundingMode.DOWN).doubleValue();
	}
	
	public function __toString() {
		return $this->team->teamName;
	} 
	
}

class Fixture {
	public $homeTeamId;
	public $awayTeamId;
	public $gameNumber;
	public $leagueId;
	public $round;
}

class FixtureRound {
	public $round;
	public $fixtures;
}

class PlayerForRound {
	public $teamId;
	public $round;
	public $playerId;
	public $replacementPlayerId;
}

class GameResult {
	public $homeTeam;
	public $awayTeam;
	public $homeScores;
	public $awayScores;
	
	public function getHomeCoachScore() {
		$homeCoachScore = 0;
		$count = count($this->homeScores);
		for ($i = 0; $i < $count; $i++) {
			$homeCoachScore += $this->homeScores[$i]->score;
		}
		
		return $homeCoachScore;
	}
	
	public function getAwayCoachScore() {
		$awayCoachScore = 0;
		$count = count($this->awayScores);
		for ($i = 0; $i < $count; $i++) {
			$awayCoachScore += $this->awayScores[$i]->score;
		}
		return $awayCoachScore;
	}
	
	public function isHomeTeamWin() {
		return $this->getHomeCoachScore() > $this->getAwayCoachScore();
	}
	
	public function isDraw() {
		return $this->getHomeCoachScore() == $this->getAwayCoachScore();
	}	
	
	public function getHomeScoresAverage() {
		$numberPlayers = 0;
		$homeCoachScore = 0;
		$count = count($this->homeScores);
		for ($i = 0; $i < $count; $i++) {
			$homeCoachScore += $homeScores[$i]->score;
			$numberPlayers++;
		}
		
		return round((floatval($homeCoachScore) / floatval($numberPlayers)), 2);
	}
	
	public function getAwayScoresAverage() {
		$numberPlayers = 0;
		$awayCoachScore = 0;
		$count = count($this->awayScores);
		for ($i = 0; $i < $count; $i++) {
			$awayCoachScore += $awayScores[$i]->score;
			$numberPlayers++;
		}
		
		return round((floatval($awayCoachScore) / floatval($numberPlayers)), 2);
	}

	public function getHomeNumberPlayersPlayed() {
		$numberOfPlayers = 0;
		$count = count($this->homeScores);
		for ($i = 0; $i < $count; $i++) {
			if ($this->homeScores[$i]->score >= 0) {
				$numberOfPlayers++;
			}
		}
		
		return $numberOfPlayers;
	}
	
	public function getAwayNumberPlayersPlayed() {
		$numberOfPlayers = 0;
		$count = count($this->awayScores);
		for ($i = 0; $i < $count; $i++) {
			if ($this->awayScores[$i]->score >= 0) {
				$numberOfPlayers++;
			}
		}
		
		return $numberOfPlayers;
	}
	
}

class KPPStatusReview {
	public $id;
	public $player;
	public $description;
	public $raiserComment;
	public $boardMemberDecisions;
	public $comments;
	
	public function getDecision($boardMember) {
		$count = count($this->boardMemberDecisions);
		for ($i = 0; $i < $count; $i++) {
			if ($this->boardMemberDecisions[$i]->boardMember == $boardMember) {
				return $this->boardMemberDecisions[$i]->decision;
			}
		}
	}
	
	public function getOverallDecision() {
		$count = count($this->boardMemberDecisions);
		$yess = 0;
		$nos = 0;
		for ($i = 0; $i < $count; $i++) {
			if ($this->boardMemberDecisions[$i]->decision == "NO") {
				$nos++;
			}
			else if ($this->boardMemberDecisions[$i]->decision == "YES") {
				$yess++;
			}
		}
		if ($yess > $nos) {
			return "YES";
		}
		else if ($yess < $nos) {
			return "NO"; 
		}
		else if ($yess == $nos && $yess != 0) {
			return "HUNG";
		}
		else {
			return "ND";
		}
	}
	
	public function getNumberDecided() {
		$count = count($this->boardMemberDecisions);
		$decided = 0;
		for ($i = 0; $i < $count; $i++) {
			if ($this->boardMemberDecisions[$i]->decision != null) {
				$decided++;
			}
		}

		return $decided;
	}	
}

class KPPStatusReviewItem {
	public $boardMember;
	public $decision;
}

class KPPStatusReviewComment {
	public $boardMember;
	public $comment;
}

class Settings {
	public $trades;
	public $timeLimit;
	public $bot;
	public $timeLength;
	public $tradesPageID;
	public $picksPageID;
	public $seasonInProgress;
	public $submittedTeamsPageID;
	public $resultsPageID;
	public $forumID;
	public $fullLockoutPenalty;
	public $partialLockoutPenalty;
}

class UserSettings {
	public $coachName;
	public $settings;
	
	public function getSettingValue($name) {
		$count = count($this->settings);
		for ($i = 0; $i < $count; $i++) {
			if ($this->settings[$i]->name == $name) {
				return $this->settings[$i]->value;
			}
		}

		return null;
	}
}

class UserSetting {
	public $name;
	public $value;
}

class FixtureItem {
	public $round;
	public $homeTeamName;
	public $awayTeamName;
	public $homeTeamId;
	public $awayTeamId;
	public $gameNumber;
	public $homePlayerScores;
	public $awayPlayerScores;
	public $usedEmergencies;
	
	public function getHomeTeamScore() {
		$count = count($this->homePlayerScores->playerScores);
		$totalScore = 0;
		for ($i = 0; $i < $count; $i++) {
			$totalScore += $this->homePlayerScores->playerScores[$i]->score;
		}
		
		return $totalScore;
	}
	
	public function getAwayTeamScore() {
		$count = count($this->awayPlayerScores->playerScores);
		$totalScore = 0;
		for ($i = 0; $i < $count; $i++) {
			$totalScore += $this->awayPlayerScores->playerScores[$i]->score;
		}
		
		return $totalScore;
	}
	
	public function hasPlayingRuckOrSpareKPPRuck($homeOrAway) {
		$playerScores = $this->homePlayerScores;
		if ($homeOrAway == "Away") {
			$playerScores = $this->awayPlayerScores;
		}
		$count = count($playerScores->playerScores);
		$totalScore = 0;
		foreach ($playerScores->playerScores as $playerScore) {
			if ($playerScore->score > 0) {
				if ($playerScore->player->playerType == PlayerType::Ruck && $playerScore->playerPosition != "R") {
					return true;
				}
				else if ($playerScore->player->playerType == PlayerType::KPP_Ruck && $playerScore->playerPosition != "R") {
					if (isDefence($playerScore->playerPosition)) {
						if ((($this->getKppDefenderCount($homeOrAway) - 1) >= 2) && (($this->getKppCount($homeOrAway) - 1) >= 4)) {
							return true;
						}
					}
					else {
						if (($this->getKppCount($homeOrAway) - 1) >= 4) {
							return true;
						}
					}
				}
			}	
		}
		
		return false;		
	}
	
	public function getKPPRuck($homeOrAway) {
		$playerScores = $this->homePlayerScores;
		if ($homeOrAway == "Away") {
			$playerScores = $this->awayPlayerScores;
		}
		$count = count($playerScores->playerScores);
		$totalScore = 0;
		foreach ($playerScores->playerScores as $playerScore) {
			if ($playerScore->player->playerType == PlayerType::KPP_Ruck && $playerScore->playerPosition != "R" && $playerScore->score > 0) {
				return $playerScore;
			}	
		}
		
		return $playerScore;
	}	
	
	public function kppRequirementsMet($homeOrAway) {
		$playerScores = $this->homePlayerScores;
		if ($homeOrAway == "Away") {
			$playerScores = $this->awayPlayerScores;
		}
		$count = count($playerScores->playerScores);
		$kppCount = 0;
		$kppDefenderCount = 0;
		$halfKppCount = 0;
		$halfKppDefenderCount = 0;
		foreach ($playerScores->playerScores as $playerScore) {
			$isReplacement = false;
			if ($playerScore->replacementPlayer != null) {
				$playerScore = $playerScore->replacementPlayer;
				$isReplacement = true;
			}
			if ($playerScore->score > 0) {
				if ($playerScore->player->playerType == PlayerType::KPP || ($playerScore->player->playerType == PlayerType::KPP_Ruck && $playerScore->playerPosition != "R")) {
					$kppCount++;
					if (isDefence($playerScore->playerPosition) || ($isReplacement && $playerScore->isKPD)) {
						$kppDefenderCount++;			
					}
				}
				else if ($playerScore->player->playerType == PlayerType::Half_KPP) {
					$halfKppCount++;
					if (isDefence($playerScore->playerPosition)) {
						$halfKppDefenderCount++;			
					}
				}
			}
		}
		
		if ($halfKppCount >= 2) {
			$kppCount++;
		}
		else if ($halfKppCount == 1) {
			$kppCount += 0.5;
		}
		if ($halfKppDefenderCount >= 2) {
			$kppDefenderCount++;
		}
		else if ($halfKppDefenderCount == 1) {
			$kppDefenderCount += 0.5;
		}
		if ($kppCount >= 4 && $kppDefenderCount >= 2) {
			return true;
		}
		
		return false;
	}	
	
	public function getKppDefenderCount($homeOrAway) {
		$playerScores = $this->homePlayerScores;
		if ($homeOrAway == "Away") {
			$playerScores = $this->awayPlayerScores;
		}
		$count = count($playerScores->playerScores);
		$kppDefenderCount = 0;
		$halfKppCount = 0;
		$halfKppDefenderCount = 0;		
		foreach ($playerScores->playerScores as $playerScore) {
			if ($playerScore->player->playerType == PlayerType::KPP || ($playerScore->player->playerType == PlayerType::KPP_Ruck && $playerScore->playerPosition != "RUCK") && isDefence($playerScore->playerPosition) && $playerScore->score > 0) {
				$kppDefenderCount++;			
			}
			else if ($playerScore->player->playerType == PlayerType::Half_KPP && isDefence($playerScore->playerPosition) && $playerScore->score > 0) {
				$halfKppDefenderCount++;			
			}
		}
		
		if ($halfKppDefenderCount >= 2) {
			$kppDefenderCount++;
		}
		else if ($halfKppDefenderCount == 1) {
			$kppDefenderCount += 0.5;
		}			
		
		return $kppDefenderCount;
	}
	
	public function getKppCount($homeOrAway) {
		$playerScores = $this->homePlayerScores;
		if ($homeOrAway == "Away") {
			$playerScores = $this->awayPlayerScores;
		}
		$count = count($playerScores->playerScores);
		$kppCount = 0;
		$halfKppCount = 0;
		foreach ($playerScores->playerScores as $playerScore) {
			if ($playerScore->player->playerType == PlayerType::KPP || ($playerScore->player->playerType == PlayerType::KPP_Ruck && $playerScore->playerPosition != "RUCK") && $playerScore->score > 0) {
				$kppCount++;			
			}
			else if ($playerScore->player->playerType == PlayerType::Half_KPP && $playerScore->score > 0) {
				$halfKppCount++;			
			}
		}
		
		if ($halfKppCount >= 2) {
			$kppCount++;
		}
		else if ($halfKppCount == 1) {
			$kppCount += 0.5;
		}			
		
		return $kppCount;
	}		
	
	public function getAwayPlayersPlayed() {
		$count = count($this->awayPlayerScores->playerScores);
		$totalPlayersPlayed = 0;
		for ($i = 0; $i < $count; $i++) {
			if ($this->awayPlayerScores->playerScores[$i]->player->playerFirstName != "Penalty") {
				$totalPlayersPlayed++;
			}
		}
		
		return $totalPlayersPlayed;
	}	
	
	public function getZeroScoringPlayers($homeOrAway) {
		$playerScores = $this->homePlayerScores;
		if ($homeOrAway == "Away") {
			$playerScores = $this->awayPlayerScores;
		}
		$zeroScoringPlayers = array();
		foreach ($playerScores->playerScores as $playerScore) {
			if ($playerScore->score == 0) {
				$zeroScoringPlayers[] = $playerScore; 
			}
		}
		
		usort($zeroScoringPlayers, "comparePlayerScoreByPlayerType");
		
		return $zeroScoringPlayers;
	}	
	
	public function getHomePlayersPlayed() {
		$count = count($this->homePlayerScores->playerScores);
		$totalPlayersPlayed = 0;
		for ($i = 0; $i < $count; $i++) {
			if ($this->homePlayerScores->playerScores[$i]->player->playerFirstName != "Penalty") {
				$totalPlayersPlayed++;
			}
		}
		
		return $totalPlayersPlayed;
	}
	
	public function isAlreadyUsed($emergencyPlayer) {
		foreach ($this->usedEmergencies as $playerScore) {
			if ($playerScore->player->getDescription() == $emergencyPlayer->player->getDescription()) {
				return true;
			}
		}
		
		return false;
	}	
	
	public function getRuckEmergency($homeOrAway) {
		$highestScoringEmergency = null;
		$playerScores = $this->homePlayerScores->nonScoringPlayers;
		if ($homeOrAway == "Away") {
			$playerScores = $this->awayPlayerScores->nonScoringPlayers;
		}
		
		$index = 0;
		
		foreach ($playerScores as $homePlayerScore) {
			if ($homePlayerScore->emergency && $homePlayerScore->score > 0 && ($homePlayerScore->player->playerType == PlayerType::Ruck || $homePlayerScore->player->playerType == PlayerType::KPP_Ruck) && !$this->isAlreadyUsed($homePlayerScore)) {
				if ($highestScoringEmergency == null || $homePlayerScore->score > $highestScoringEmergency->score) {
					$highestScoringEmergency = $homePlayerScore;
				}
			}
			
			$index++;			
		}
		
		return $highestScoringEmergency;
	}	
	
	public function getKPDEmergency($homeOrAway) {
		$highestScoringEmergency = null;
		$playerScores = $this->homePlayerScores->nonScoringPlayers;
		if ($homeOrAway == "Away") {
			$playerScores = $this->awayPlayerScores->nonScoringPlayers;
		}
		
		$index = 0;
		
		foreach ($playerScores as $homePlayerScore) {
			if ($homePlayerScore->emergency && $homePlayerScore->score > 0 && $homePlayerScore->player->playerType == PlayerType::KPP && $homePlayerScore->isKPD && !$this->isAlreadyUsed($homePlayerScore)) {
				if ($highestScoringEmergency == null || $homePlayerScore->score > $highestScoringEmergency->score) {
					$highestScoringEmergency = $homePlayerScore;
				}
			}
			$index++;				
		}
		
		return $highestScoringEmergency;
	}	
	
	public function getHalfKPDEmergency($homeOrAway) {
		$highestScoringEmergency = null;
		$playerScores = $this->homePlayerScores->nonScoringPlayers;
		if ($homeOrAway == "Away") {
			$playerScores = $this->awayPlayerScores->nonScoringPlayers;
		}
		
		$index = 0;
		
		foreach ($playerScores as $homePlayerScore) {
			if ($homePlayerScore->emergency && $homePlayerScore->score > 0 && $homePlayerScore->player->playerType == PlayerType::Half_KPP && $homePlayerScore->isKPD && !$this->isAlreadyUsed($homePlayerScore)) {
				if ($highestScoringEmergency == null || $homePlayerScore->score > $highestScoringEmergency->score) {
					$highestScoringEmergency = $homePlayerScore;
				}
			}
			$index++;				
		}
		
		return $highestScoringEmergency;
	}	
	
	public function haveKPPRuckDefenderInRuck($homeOrAway) {
		$highestScoringEmergency = null;
		$playerScores = $this->homePlayerScores->playerScores;
		if ($homeOrAway == "Away") {
			$playerScores = $this->awayPlayerScores->playerScores;
		}
		
		foreach ($playerScores as $homePlayerScore) {
			if ($homePlayerScore->score > 0 && $homePlayerScore->player->playerType == PlayerType::KPP_Ruck && $homePlayerScore->isKPD && $homePlayerScore->player->playerPosition != "RUCK") {
				return true;
			}			
		}
		
		return false;
	}	
	
	public function haveKPPRuckInRuck($homeOrAway) {
		$highestScoringEmergency = null;
		$playerScores = $this->homePlayerScores->playerScores;
		if ($homeOrAway == "Away") {
			$playerScores = $this->awayPlayerScores->playerScores;
		}
		
		foreach ($playerScores as $homePlayerScore) {
			if ($homePlayerScore->score > 0 && $homePlayerScore->player->playerType == PlayerType::KPP_Ruck && $homePlayerScore->player->playerPosition != "RUCK") {
				return true;
			}			
		}
		
		return false;
	}	
	
	public function getKPPEmergency($homeOrAway) {
		$highestScoringEmergency = null;
		$playerScores = $this->homePlayerScores->nonScoringPlayers;
		if ($homeOrAway == "Away") {
			$playerScores = $this->awayPlayerScores->nonScoringPlayers;
		}
		
		$index = 0;
		
		foreach ($playerScores as $homePlayerScore) {
			if ($homePlayerScore->emergency && $homePlayerScore->score > 0 && $homePlayerScore->player->playerType == PlayerType::KPP && !$this->isAlreadyUsed($homePlayerScore)) {
				if ($highestScoringEmergency == null || $homePlayerScore->score > $highestScoringEmergency->score) {
					$highestScoringEmergency = $homePlayerScore;
				}
			}
			$index++;			
		}
		
		return $highestScoringEmergency;
	}	
	
	public function getHalfKPPEmergency($homeOrAway) {
		$highestScoringEmergency = null;
		$playerScores = $this->homePlayerScores->nonScoringPlayers;
		if ($homeOrAway == "Away") {
			$playerScores = $this->awayPlayerScores->nonScoringPlayers;
		}
		
		$index = 0;
		
		foreach ($playerScores as $homePlayerScore) {
			if ($homePlayerScore->emergency && $homePlayerScore->score > 0 && $homePlayerScore->player->playerType == PlayerType::Half_KPP && !$this->isAlreadyUsed($homePlayerScore)) {
				if ($highestScoringEmergency == null || $homePlayerScore->score > $highestScoringEmergency->score) {
					$highestScoringEmergency = $homePlayerScore;
				}
			}
			$index++;			
		}
		
		return $highestScoringEmergency;
	}	
	
	public function getHighestScoringEmergency($homeOrAway) {
		$playerScores = $this->homePlayerScores->nonScoringPlayers;
		if ($homeOrAway == "Away") {
			$playerScores = $this->awayPlayerScores->nonScoringPlayers;
		}
		$highestScoringEmergency = null;
		$index = 0;
		foreach ($playerScores as $playerScore) {
			if ($playerScore->emergency && $playerScore->score > 0 && !$this->isAlreadyUsed($playerScore)) {
				if ($highestScoringEmergency == null || $playerScore->score > $highestScoringEmergency->score) {
					$highestScoringEmergency = $playerScore;
				}
			}
			$index++;
		}
		
		return $highestScoringEmergency;
	}	
}

class PlayerScore {
	public $playerId;
	public $player;
	public $score;
	public $replacementPlayerId;
	public $replacementPlayer;
	public $replacementDescription;
	public $replacementFor;
	public $emergency;
	public $positionInAFLTeam;
	public $playerPosition;
	public $isInValid;
	public $isKPD;	
	public $traded;
}

class PlayerScores {
	public $playerScores;
	public $nonScoringPlayers;
}

class TeamPlayerScores {
	public $team;
	public $playerScores;
	
	public function getAverageTotal() {
		$count = count($this->playerScores);
		if ($count == 0) return 0;
		$totalPlayers = 0;
		$totalScore = 0;
		for ($i = 0; $i < $count; $i++) {
			if ($this->playerScores[$i]->score > 0) {
				$totalScore += $this->playerScores[$i]->score;
				$totalPlayers++;
			}
		}
		
		if ($totalPlayers == 0) {
			return 0;
		}
		
		return round(floatval($totalScore) / floatval($totalPlayers));
	}
	
	public function getAverageTop15() {
		$count = count($this->playerScores);
		if ($count == 0) return 0;
		$totalScore = 0;
		$totalPlayers = 0;
		for ($i = 0; $i < $count && $i < 15; $i++) {
			$totalScore += $this->playerScores[$i]->score;
			$totalPlayers++;
		}
		
		if ($totalPlayers == 0) {
			return 0;
		}
		
		return round(floatval($totalScore) / floatval($totalPlayers));
	}	
	
	public function getAverageAbove30() {
		$count = count($this->playerScores);
		if ($count == 0) return 0;
		$totalScore = 0;
		$totalPlayers = 0;
		for ($i = 0; $i < $count && $i < $count; $i++) {
			if ($this->playerScores[$i]->score >= 30) {
				$totalScore += $this->playerScores[$i]->score;
				$totalPlayers++;
			}
		}
		
		if ($totalPlayers == 0) {
			return 0;
		}
		
		return round(floatval($totalScore) / floatval($totalPlayers));
	}	
}

class PlayerReplacementName {
	public $firstName;
	public $surname;
	public $team;
	public $requiredFirstName;
	public $requiredSurname;
	
	function __construct($firstName, $surname, $team, $requiredFirstName, $requiredSurname) {
		$this->firstName = $firstName;
		$this->surname = $surname;
		$this->team = $team;
		$this->requiredFirstName = $requiredFirstName;
		$this->requiredSurname = $requiredSurname;
	}
}

class Config {
	public $server;
	public $username;
	public $password;
	public $schema;
	public $hours;
}

class ErrorEntry {
	public $firstName;
	public $surname;
	public $score;
	public $process;
	public $dateTime;
}

class TimeToPerformTask {
	public $day;
	public $hours;
	public $minutes;
	public $task;
}

class RoundDetails {
	public $selectedTeamsReleaseDays;
	public $property;
}

class InvalidTeam {
	public $coachName;
	public $invalidPlayers;
}
?>